using UnityEngine;
using System.Collections;
using System;

public class load_char : MonoBehaviour {
	
	private int qtdChar; 
	public GameObject Character;
	public Transform Posicao;
	
	// Use this for initialization
	void Start () {
		    StartCoroutine(contChar_());
	}
	
	IEnumerator contChar_(){
		
		WWWForm form = new WWWForm();
		
		form.AddField("idLogin", constant_sistem.idUsuari_Atual);
						
		WWW webRequest = new WWW(constant_sistem.db_url + "character_cont.php", form);
		
		yield return webRequest;
		
		if (webRequest.error !=null) {
			Debug.Log(webRequest.error);
		} else { 
			qtdChar = Convert.ToInt32(webRequest.text);
			
			int count = 1;

			if(qtdChar > 0){
				WWWForm form2 = new WWWForm();
		
				form2.AddField("idLogin", constant_sistem.idUsuari_Atual);
						
				WWW webRequest2 = new WWW(constant_sistem.db_url + "character_id.php", form);
		
				yield return webRequest2;
				
				if (webRequest.error !=null) {
					Debug.Log(webRequest.error);
				} else {					
					string todosId = webRequest2.text;
					string[] idChar = todosId.Split(';');
					
					while (count < qtdChar + 1){
						if (count == 1){
							Character.name = idChar[0];
							Instantiate(Character, new Vector3(constant_sistem.Char1_position.position.x,constant_sistem.Char1_position.position.y,
												   constant_sistem.Char1_position.position.z), Quaternion.identity);							
						} else 
						if (count == 2){
							Character.name = idChar[1];
							Instantiate(Character, new Vector3(constant_sistem.Char2_position.position.x,constant_sistem.Char2_position.position.y,
												   constant_sistem.Char2_position.position.z), Quaternion.identity);						
						} else
						if (count == 3){
							Character.name = idChar[2];
							Instantiate(Character, new Vector3(constant_sistem.Char3_position.position.x,constant_sistem.Char3_position.position.y,
												   constant_sistem.Char3_position.position.z), Quaternion.identity);
						} else
						if (count == 4){
							Character.name = idChar[3];
							Instantiate(Character, new Vector3(constant_sistem.Char4_position.position.x,constant_sistem.Char4_position.position.y,
												   constant_sistem.Char4_position.position.z), Quaternion.identity);
						} else
						if (count == 5){
							Character.name = idChar[4];
							Instantiate(Character, new Vector3(constant_sistem.Char5_position.position.x,constant_sistem.Char5_position.position.y,
												   constant_sistem.Char5_position.position.z), Quaternion.identity);
						}
						count++;
					}			
				}
			}
		}
	}
	
}
